$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/users/wallet/list',
        datatype: "json",
        colModel: [
			{ label: 'ID', name: 'id', index: 'id', width: 60, key: true},
			{ label: '用户ID', name: 'userId', index: 'user_id', width: 60},
			{ label: '手机号', name: 'mobile', index: 'mobile', width: 120},
			{ label: '币种名称', name: 'symbol', index: 'symbol', width: 160 },
			{ label: '币种标识', name: 'slip44', index: 'slip44', width: 160 },
			{ label: 'ERC 20 地址', name: 'address', index: 'address', width: 160 },
			{ label: '可用余额', name: 'amount', index: 'amount', width: 160 },
			{ label: '冻结余额', name: 'amountLock', index: 'amount_lock', width: 160 },
			{ label: '更新时间', name: 'updateTime', index: 'update_time', width: 160 }
        ],
		height: 'auto',
		shrinkToFit: false,
		autoScroll: true,
		viewrecords: true,
		rowNum: 10,
		rowList: [10, 30, 50],
		rownumbers: true,
		rownumWidth: 25,
		autowidth: true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader: {
			root: "page.list",
			page: "page.currPage",
			total: "page.totalPage",
			records: "page.totalCount"
		},
		prmNames: {
			page: "page",
			rows: "limit",
			order: "order"
		},
		gridComplete: function () {
			//隐藏grid底部滚动条
			$("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-y": "hidden"});
		}
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			userId: null,
			mobile: null
		},
		showList: true,
		title: null,
		userWallet: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.userWallet = {};
		},
		update: function (event) {
			// getSelectedRow;
			var userId = getSelectedRow();
			if(userId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(userId)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = "sys/users/wallet/saveOrUpdate";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.userWallet),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		getInfo: function(userId){
			$.get(baseURL + "sys/users/wallet/info/"+userId, function(r){
                vm.userWallet = r.userWallet;
				vm.userWallet.amount = 0;
            });
		},

		reset:function(){
			vm.q = {userId:null,mobile:null};
			$("#combo").val('');
			$('table td input').val('');
			vm.reload();
		},

		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			var parmas=	{'userId':vm.q.userId,'mobile':vm.q.mobile};
			$("#jqGrid").jqGrid('setGridParam',{
				postData:parmas,
				page:1
			}).trigger("reloadGrid");
		},

		exportList:function(){
			vm.q = {userId:vm.q.userId, mobile:vm.q.mobile};
			var url = "sys/users/wallet/exportList";
			var f = document.createElement("form");
			document.body.appendChild(f);
			var input = document.createElement("input");
			input.type = "hidden";
			f.appendChild(input);
			input.name = "param";
			input.value = JSON.stringify(vm.q);
			f.method="POST";
			f.action = baseURL + url;
			f.submit();
			f.remove();
		}
	}
});

function audit(rowId, status) {
    $("#btn_no").show();
    $("#btn_ok").show();
    $("#btn_remove").show();
    var rowData = jQuery("#jqGrid").jqGrid("getRowData", rowId);
    $("#failCauseDiv").hide();
    if (status === 2) {
        $("#btn_no").hide();
        $("#btn_ok").hide();
    }
    $("#hiddenId").val(rowData.id);
    $("#frontImgUrl").html(rowData.frontImgUrl);
    $("#backImgUrl").html(rowData.backImgUrl);
    if (status === 3) {
        $("#failCauseDiv").show();
        $("#failCause").html(rowData.auditRemark);
    }
    $('#myModal').modal();
}

function success() {
    var id=$("#hiddenId").val();
    auditResult(2,id,"");
}

function refuse() {
    $('#failCauseModal').modal();
}

function savefailCause() {
    var id=$("#hiddenId").val();
    var failCause=$("#failCause1").val();
    if (failCause == null || failCause === ''){
        alert("请输入拒绝理由");
        return;
    }
    auditResult(3,id,failCause);
}

function auditResult(status,id,failCause){
    url = baseURL + "sys/rebirth/user/audit?id=" + id + "&status=" + status + "&failCause=" + failCause;
    $.ajax({
        type: "POST",
        url: url,
        contentType: "application/json",
        success: function(r){
            if(r.code === 0){
                alert('操作成功', function(){
                    vm.reload();
                });
            }else{
                alert(r.msg);
            }
        }
    });
}

function showImg(val){
	$("#showImg").attr("src",val);
	layer.open({
		type: 1,
		skin: 'layui-layer-molv',
		title: "身份证",
		area: ['650px', '500px'],
		shade: 0,
		shadeClose: false,
		content: jQuery("#Img"),
		btn: ['确定'],
		btn1: function (index) {
			layer.close(index);
		}
	});
}